package com.jg.visitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jg.visitor.annotation.LoginCheck;
import com.jg.visitor.common.bean.PageDTO;
import com.jg.visitor.common.bean.ServiceCode;
import com.jg.visitor.common.utils.DateTimeUtil;
import com.jg.visitor.common.utils.LoginHelper;
import com.jg.visitor.config.exception.BaseException;
import com.jg.visitor.config.exception.JgException;
import com.jg.visitor.controller.dto.BlacklistDTO;
import com.jg.visitor.controller.dto.UserDTO;
import com.jg.visitor.controller.enums.BlacklistOperationEnum;
import com.jg.visitor.controller.vo.BlackListQueryVO;
import com.jg.visitor.domain.Blacklist;
import com.jg.visitor.domain.VisitorInfo;
import com.jg.visitor.service.BlacklistService;
import com.jg.visitor.mapper.BlacklistMapper;
import com.jg.visitor.service.VisitorInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 小狐狸
针对表【blacklist】的数据库操作Service实现
2022-05-28 21:46:48
*/
@Service
public class BlacklistServiceImpl extends ServiceImpl<BlacklistMapper, Blacklist> implements BlacklistService{

    @Resource
    private BlacklistMapper blacklistMapper;
    @Resource
    private VisitorInfoService visitorInfoService;


    @Override
    public PageDTO<BlacklistDTO> adminSelectBlacklistByCondition(BlackListQueryVO queryVO) {

        PageDTO<BlacklistDTO> retDto = new PageDTO<>();

        if(queryVO.getPage() != null && queryVO.getPageSize() != null){
            Page<Blacklist> page = new Page<>(queryVO.getPage(),queryVO.getPageSize());
            List<BlacklistDTO> result = blacklistMapper.selectBlacklistByCondition(page,queryVO);
            retDto.setTotal(page.getTotal());
            retDto.setPage(queryVO.getPage());
            retDto.setPageSize(queryVO.getPageSize());
            retDto.setData(result);
        }
        else{
            List<BlacklistDTO> result = blacklistMapper.selectBlacklistByCondition(queryVO);
            retDto.setTotal(result.size());
            retDto.setData(result);
        }


        return retDto;
    }

    @Override
    @LoginCheck
    public boolean addVisitorToBlacklist(Long visitorId,Integer operationType) {
        if(visitorId == null){
            throw new JgException(ServiceCode.BAD_REQUEST);
        }

        VisitorInfo visitorInfo = visitorInfoService.getById(visitorId);

        if(null == visitorInfo){
            throw new BaseException(-1,"未找到此用户信息！");
        }

        UserDTO loginUser = LoginHelper.getLoginUser();
        if(loginUser == null){
            throw new BaseException(-1,"管理员信息异常！");
        }

        
        if(BlacklistOperationEnum.ADD_BLACKLIST.getOperationCode().equals(operationType)){
            int count = this.count(new QueryWrapper<Blacklist>().eq("visitor_id",visitorId));
            if(count > 0){
                throw new BaseException(-1,"已经拉黑不能再次操作！");
            }

            Blacklist blacklist = new Blacklist();
            blacklist.setGmtCreate(DateTimeUtil.getTimeNow());
            blacklist.setGmtModified(DateTimeUtil.getTimeNow());
            blacklist.setVisitorId(visitorId);
            blacklist.setVisitorName(visitorInfo.getUsername());
            blacklist.setCardNumber(visitorInfo.getCardNumber());
            blacklist.setOperationUserId(loginUser.getId());
            blacklist.setOperationUserName(loginUser.getNickname());


            return blacklist.insert();
        }

        if(BlacklistOperationEnum.REMOVE_BLACKLIST.getOperationCode().equals(operationType)){
            return this.remove(new QueryWrapper<Blacklist>().eq("visitor_id",visitorId));
        }

        throw new BaseException(-1,"操作类型异常");
    }
}




